<?php
class NewsModel extends BaseModel{
    //data passed o the News index view
    public function index(){
 
        $News = $this->find_all($table = 'news AS n LEFT JOIN categories AS c ON n.category_id = c.category_id', $orderby = 'news_id DESC', $where='', $cols='n.status, n.news_id, n.title, n.content, c.category_name');

        $this->viewModel->set("pageTitle","News Manager");
        $this->viewModel->set("News", $News);

        $categories=$this->find_all("categories", "category_id DESC","");
        $this->viewModel->set("categories",$categories);
        
        return $this->viewModel;
    }
    
    public function create($data){
        if(!empty($data)){
            return $this->save($table ='news', $data = $data, $where='');
        }
        $categories=$this->find_all("categories", "category_id DESC","");
        $this->viewModel->set("categories",$categories);
        $this->viewModel->set("pageTitle","Create News");
        return $this->viewModel;
    }
    
    public function edit($news_id, $data){
        //update item when have data
        if(!empty($data)){
            return $this->save($table='news', $data=$data, $where='news_id='.$news_id);
        }
        //get item detail by subject_id
        $news = $this->find($table='news', $orderby='news_id DESC', $where='news_id='.$news_id, $cols='*', $limit='10');
        $categories=$this->find_all("categories","category_id DESC","","*");
        
        $this->viewModel->set("pageTitle","Update News");
        $this->viewModel->set("news",$news);
        $this->viewModel->set("category",$categories);
        return $this->viewModel;
    }
    
    public function selectMulti($query)
    {
        $this->query($query);
        $data = $this->get();
        $data = (empty($data)) ? array() : $data;
        $this->viewModel->set("data",$data);
        return $this->viewModel;
    }
} 
   
   